// 导入持久化方法
// import { getToken, setToken, removeToken } from '@/utils/auth.js'
import * as auth from '@/utils/auth.js'
import { getUserDetailById, getUserInfo } from '@/api/user'
export default {
  namespaced: true,
  state: {
    // token: '',取token从本地取
    token: auth.getToken() || '',
    userInfo: {}
  },
  mutations: {
    // state是个对象，token存token时传进来的值
    setToken (state, token) {
      state.token = token
      auth.setToken(token)
    },
    delToken (state) {
      state.token = ''
      auth.removeToken()
    },
    setUserInfo (state, userInfo) {
      state.userInfo = userInfo
    },
    delUser (state) {
      state.userInfo = {}
    }
  },
  actions: {
    async getUserAction ({ commit }) {
      const user = await getUserInfo()
      // console.log(user)
      // 信息在两个返回值里
      const info = await getUserDetailById(user.userId)
      // console.log(info)
      commit('setUserInfo', { ...user, ...info })
    },
    logoutAction ({ commit }) {
      commit('delToken')
      commit('delUser')
    }
  }
}

